package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.ProjectBean;
import com.wnhz.work.yz.bean.ProjectChild1;
import com.wnhz.work.yz.molder.SpecialShopMolder;

import org.xutils.x;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品特批信息
 */
public class SpecialShopActivity extends BaseActivity implements View.OnClickListener {
    private final int SPECIAL_SHOP_TAG = 661;
    private List<SpecialShopMolder> mList = new ArrayList<>();
    private BaseRVAdapter<SpecialShopMolder> adapter;

    private RecyclerView recyclerView;
    private View foot_view;
    private View lay1, lay2;
    private List<ProjectChild1> proojectList;
    private Double discount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_shop);
        initTitle();
        initDate();
        foot_view = LayoutInflater.from(this).inflate(R.layout.foot_special_shop, null);
        lay1 = foot_view.findViewById(R.id.lay1);
        lay2 = foot_view.findViewById(R.id.lay3);
        lay2.setOnClickListener(this);

        showOrHide();
        final DecimalFormat df = new DecimalFormat("######0.00");
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new BaseRVAdapter<SpecialShopMolder>(this, mList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_special_shop;
            }

            @Override
            public void onBind(final BaseViewHolder holder, final int position) {
                final SpecialShopMolder bean = mList.get(position);
                Log.e("----", "" + bean.getTitle_tv());
                x.image().bind(holder.getImageView(R.id.shopping_img), bean.getIv_img());
                holder.setTextView(R.id.title_tv, bean.getTitle_tv() + "");
                holder.setTextView(R.id.number_tv, "×" + bean.getNumber_tv() + "");
                holder.setTextView(R.id.material, bean.getMaterial() + "");
                holder.setTextView(R.id.tv_price, bean.getTv_price() + "");
                mList.get(position).setTePiPrice(holder.getTextView(R.id.tv_price).getText() + "");
                final TextView remission = holder.getView(R.id.price_ed);
                //
                Log.e("--" + position, mList.get(position).getTepi() + "");
                if (mList.get(position).getTepi() != 0)
                    remission.setText(mList.get(position).getTepi() + "");
                mList.get(position).setOther_tepi(0 + "");

                Double tepiPrice = 0.0;
                try {
                    tepiPrice = Double.parseDouble(holder.getTextView(R.id.tv_price).getText().toString()) - Double.parseDouble(remission.getText().toString());
                    mList.get(position).setTepi(Double.parseDouble(remission.getText().toString()));
                } catch (Exception e) {
                    tepiPrice = 0.0;
                    mList.get(position).setTepi(0);
                }
                if (tepiPrice < 0) {
                    tepiPrice = 0.0;
                }
                try {
                    mList.get(position).setTePiPrice(Double.parseDouble(remission.getText().toString()) + "");
                } catch (Exception e) {
                    mList.get(position).setTePiPrice(holder.getTextView(R.id.tv_price).getText() + "");
                }

                holder.setTextView(R.id.remission, "¥ " + df.format(tepiPrice * Double.parseDouble(mList.get(position).getNumber_tv())));
                mList.get(position).setOther_tepi(df.format(tepiPrice * Double.parseDouble(mList.get(position).getNumber_tv())) + "");
                holder.getView(R.id.price_ed).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final AlertDialog.Builder builder = new AlertDialog.Builder(SpecialShopActivity.this);
                        View view = View.inflate(SpecialShopActivity.this, R.layout.dialog_edit, null);
                        TextView queding1 = (TextView) view.findViewById(R.id.queding1);
                        TextView quxaio1 = (TextView) view.findViewById(R.id.quxiao1);
                        final EditText shopping_number = (EditText) view.findViewById(R.id.shopping_number);
                        TextView dialog_title = (TextView) view.findViewById(R.id.dialog_title);
                        dialog_title.setText("填写特批价格");
                        shopping_number.setText(remission.getText() + "");
                        //
                        builder.setView(view);
                        final AlertDialog show = builder.show();
                        Window dialogWindow = show.getWindow();
                        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                        dialogWindow.setGravity(Gravity.CENTER);
                        dialogWindow.setAttributes(lp);
                        queding1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Double nums = mList.get(position).getTepi();
                                try {
                                    nums = Double.parseDouble(shopping_number.getText().toString());
                                } catch (Exception e) {
                                    nums = mList.get(position).getTepi();
                                }
                                mList.get(position).setTepi(nums);
                                DecimalFormat df = new DecimalFormat("######0.00");
                                remission.setText(df.format(nums) + "");

                                Double tepiPrice = 0.0;
                                try {
                                    tepiPrice = Double.parseDouble(holder.getTextView(R.id.tv_price).getText().toString()) - Double.parseDouble(remission.getText().toString());
                                } catch (Exception e) {
                                    tepiPrice = 0.0;
                                }
                                if (tepiPrice < 0) {
                                    tepiPrice = 0.0;
                                }
                                try {
                                    mList.get(position).setTePiPrice(Double.parseDouble(shopping_number.getText().toString()) + "");
                                } catch (Exception e) {
                                    mList.get(position).setTePiPrice(holder.getTextView(R.id.tv_price).getText() + "");
                                }

                                holder.setTextView(R.id.remission, "¥ " + df.format(tepiPrice * Double.parseDouble(mList.get(position).getNumber_tv())));
                                mList.get(position).setOther_tepi(tepiPrice * Double.parseDouble(mList.get(position).getNumber_tv()) + "");
//


                                notifyDataSetChanged();
                                show.dismiss();
                            }
                        });
                        quxaio1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                show.dismiss();
                            }
                        });

                    }
                });


            }
        };
        adapter.addFooterView(foot_view);
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL, R.drawable.line_shape));
        recyclerView.setAdapter(adapter);
    }

    private ProjectBean bean;
    private String allMoney;

    private void initDate() {


        proojectList = (List<ProjectChild1>) getIntent().getSerializableExtra("dataList");
        bean = (ProjectBean) getIntent().getSerializableExtra("bean");
        mList = (List<SpecialShopMolder>) getIntent().getSerializableExtra("mList222");
//        if (mList != null) {
//            MyToast(mList.size()+"");
//        } else {
//            MyToast(mList + "lll");
//        }
        allMoney = getIntent().getStringExtra("allMoney");
        discount = Double.parseDouble(getIntent().getStringExtra("discount"));
    }

    private void showOrHide() {
        if (mList.size() == 0) {
            lay1.setVisibility(View.GONE);
        } else {
            lay1.setVisibility(View.VISIBLE);
        }
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.special_shop_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_right).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay3://选择
                ArrayList<Integer> ints = new ArrayList<>();
                for (SpecialShopMolder molder : mList) {
                    ints.add(molder.getId());
//                    Log.e("----",ints.get(i))
                }
                ArrayList<ProjectChild1> dataList1 = (ArrayList<ProjectChild1>) proojectList;
                startActivityForResult(new Intent(this, SpecialShopListActivity.class)
                        .putExtra("ints", ints).putExtra("dataList", dataList1), SPECIAL_SHOP_TAG);
                break;
            case R.id.btn_main_left:
                showDialogv7("返回数据不会保存哦", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                break;
            case R.id.tv_right:
                //MyToast("保存");
                for (int i = 0; i < mList.size(); i++) {
                    double d = Double.parseDouble(mList.get(i).getTePiPrice()) / Double.parseDouble(mList.get(i).getTv_price());
                    Log.e("----", mList.get(i).getTePiPrice() + "---" + mList.get(i).getTv_price() + "---" + mList.get(i).getTepi());
                    if (d < discount) {
                        MyToast("特批价格不能小于" + discount * 100 + "%");
                        return;
                    }
                }

                ArrayList<ProjectChild1> dataList2 = (ArrayList<ProjectChild1>) proojectList;
                Intent intent = new Intent();
                ArrayList<SpecialShopMolder> mList = (ArrayList<SpecialShopMolder>) this.mList;
                //   MyToast("..." + mList.size());
                intent.putExtra("specialShop", mList).putExtra("tePiFlag", true).putExtra("dataList", dataList2)
                        .putExtra("bean", bean).putExtra("allMoney", allMoney);
                setResult(1, intent);
                finish();
                break;

        }
    }

    String TAG = getClass().getName();


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case SPECIAL_SHOP_TAG:
                if (resultCode == RESULT_OK) {
                    SpecialShopMolder molder = (SpecialShopMolder) data.getSerializableExtra("molder");
                    molder.setTepi(0);
                    mList.add(molder);
                    showOrHide();
                    adapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
